<script lang="ts" setup>
import type { UserApi } from '#/api/types';

import { ref } from 'vue';

import { useVbenDrawer } from '@vben/common-ui';

import { message } from 'ant-design-vue';

import { useVbenForm } from '#/adapter/form';
import { updateUserInfoApi } from '#/api/core/modules/user';
import { $t } from '#/locales';

const emits = defineEmits(['success']);

// 用户数据
const userData = ref<null | UserApi.UserInfoDetailVO>(null);

// 编辑用户表单
const [Form, formApi] = useVbenForm({
  schema: [
    {
      component: 'Input',
      fieldName: 'username',
      label: $t('system.user.username'),
      componentProps: {
        placeholder: '请输入用户名',
        disabled: true,
      },
    },
    {
      component: 'Input',
      fieldName: 'nickname',
      label: $t('system.user.nickname'),
      componentProps: {
        placeholder: '请输入昵称',
      },
    },
    {
      component: 'Input',
      fieldName: 'email',
      label: $t('system.user.email'),
      componentProps: {
        placeholder: '请输入邮箱',
      },
    },
    {
      component: 'Input',
      fieldName: 'phone',
      label: $t('system.user.phone'),
      componentProps: {
        placeholder: '请输入手机号',
      },
    },
    {
      component: 'RadioGroup',
      componentProps: {
        buttonStyle: 'solid',
        options: [
          { label: $t('common.enabled'), value: 1 },
          { label: $t('common.disabled'), value: 0 },
        ],
        optionType: 'button',
      },
      fieldName: 'status',
      label: $t('system.user.status'),
    },
    {
      component: 'Upload',
      fieldName: 'avatar',
      label: $t('system.user.avatar'),
      componentProps: {
        listType: 'picture-card',
        accept: 'image/*',
      },
    },
  ],
  showDefaultActions: false,
});

// 抽屉组件
const [Drawer, drawerApi] = useVbenDrawer({
  title: $t('common.edit', [$t('system.user.name')]),

  // 表单确认处理
  async onConfirm() {
    try {
      if (!userData.value) {
        message.error('用户数据不存在');
        return false;
      }

      // 表单验证
      const { valid } = await formApi.validate();
      if (!valid) return false;

      // 获取表单值
      const values = await formApi.getValues();

      // 锁定抽屉
      drawerApi.setState({ loading: true });

      // 编辑用户
      const updateParams: UserApi.UpdateParams = {
        id: userData.value.id,
        nickname: values.nickname,
        email: values.email,
        phone: values.phone,
        avatar: values.avatar,
      };

      const result = await updateUserInfoApi(updateParams);
      if (result.success) {
        message.success($t('common.operationSuccess'));
        emits('success');

        // 主动关闭抽屉
        drawerApi.close();
        return true;
      } else {
        return false;
      }
    } catch {
      return false;
    } finally {
      drawerApi.setState({ loading: false });
    }
  },

  // 打开关闭处理
  onOpenChange(isOpen) {
    if (isOpen) {
      const data = drawerApi.getData<UserApi.UserInfoDetailVO>();
      if (data && data.id) {
        userData.value = data;
        formApi.setValues(data);
      } else {
        message.error('无效的用户数据');
        drawerApi.close();
      }
    }
  },
});

// 对外暴露抽屉API
defineExpose({ drawerApi });
</script>

<template>
  <Drawer>
    <Form />
  </Drawer>
</template>
